local chongwei = fk.CreateSkill {

  name = "joy__chongwei",

  tags = {  },

}



chongwei:addEffect(fk.GameStart, {
  name = "joy__chongwei",
  events = {fk.GameStart, fk.TurnStart, fk.Damaged,fk.RoundEnd},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if  not player:hasSkill(chongwei.name) or (#id == 0 and event ~= fk.GameStart) then
      return false
    end
    if event == fk.GameStart then
      return true
    elseif event == fk.TurnStart then
      return target == player or target:getMark("@@joy__powei_wei") > 0
    elseif event == fk.Damaged then
      return target:getMark("@@joy__powei_wei") > 0
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, nil)
    if event == fk.TurnStart and target:getMark("@@joy__powei_wei") > 0 then
      local room = player.room
      local choices = { "Cancel" }
      if target.hp <= player.hp and target ~= player and target:getHandcardNum() > 0 then
        table.insert(choices, 1, "joy__powei_prey")
      end
      if table.find(player:getCardIds(Player.Hand), function(id)
        return not player:prohibitDiscard(Fk:getCardById(id))
      end) then
        table.insert(choices, 1, "joy__powei_damage")
      end
      if #choices == 1 then
        return false
      end
      local choice = room:askForChoice(player, choices, chongwei.name)
      if choice == "Cancel" then
        return false
      end
      if choice == "joy__powei_damage" then
        local cardIds = room:askForDiscard(player, 1, 1, false, chongwei.name, true, nil, "#joy__powei-damage::" .. target.id, true)
        if #cardIds == 0 then
          return false
        end
        event:setCostData(self, cardIds[1])
      else
        event:setCostData(self, choice)
      end
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, chongwei.name)
      player:broadcastSkillInvoke(chongwei.name, 1)
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:setPlayerMark(p, "@@joy__powei_wei", 1)
      end
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    elseif event == fk.TurnStart then
      if target == player then
        if table.find(room.alive_players, function(p)
          return p:getMark("@@joy__powei_wei") > 0
        end) then
          room:notifySkillInvoked(player, chongwei.name)
          player:broadcastSkillInvoke(chongwei.name, 1)
          local hasLastPlayer = false
          for _, p in ipairs(room:getAlivePlayers()) do
            if p:getMark("@@joy__powei_wei") > (hasLastPlayer and 1 or 0) and not (#room.alive_players < 3 and p:getNextAlive() == player) then
              hasLastPlayer = true
              room:removePlayerMark(p, "@@joy__powei_wei")
              local nextPlayer = p:getNextAlive()
              if nextPlayer == player then
                nextPlayer = player:getNextAlive()
              end
              room:addPlayerMark(nextPlayer, "@@joy__powei_wei")
            else
              hasLastPlayer = false
            end
          end
        end
      end
      if type(event:getCostData(self)) == "number" then
        room:notifySkillInvoked(player, chongwei.name, "offensive")
        player:broadcastSkillInvoke(chongwei.name, 1)
        room:throwCard({ event:getCostData(self) }, chongwei.name, player, player)
        room:damage({
          from = player,
          to = target,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = chongwei.name,
        })
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      elseif event:getCostData(self) == "joy__powei_prey" then
        room:notifySkillInvoked(player, chongwei.name, "control")
        player:broadcastSkillInvoke(chongwei.name, 1)
        local cardId = room:askForCardChosen(player, target, "h", chongwei.name)
        room:obtainCard(player, cardId, false, fk.ReasonPrey)
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      end
    elseif event == fk.Damaged then
      room:setPlayerMark(target, "@@joy__powei_wei", 0)
    elseif event == fk.RoundEnd then
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    end
  end,
})
chongwei:addEffect(fk.TurnStart, {
  name = "joy__chongwei",
  events = {fk.GameStart, fk.TurnStart, fk.Damaged,fk.RoundEnd},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if  not player:hasSkill(chongwei.name) or (#id == 0 and event ~= fk.GameStart) then
      return false
    end
    if event == fk.GameStart then
      return true
    elseif event == fk.TurnStart then
      return target == player or target:getMark("@@joy__powei_wei") > 0
    elseif event == fk.Damaged then
      return target:getMark("@@joy__powei_wei") > 0
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, nil)
    if event == fk.TurnStart and target:getMark("@@joy__powei_wei") > 0 then
      local room = player.room
      local choices = { "Cancel" }
      if target.hp <= player.hp and target ~= player and target:getHandcardNum() > 0 then
        table.insert(choices, 1, "joy__powei_prey")
      end
      if table.find(player:getCardIds(Player.Hand), function(id)
        return not player:prohibitDiscard(Fk:getCardById(id))
      end) then
        table.insert(choices, 1, "joy__powei_damage")
      end
      if #choices == 1 then
        return false
      end
      local choice = room:askForChoice(player, choices, chongwei.name)
      if choice == "Cancel" then
        return false
      end
      if choice == "joy__powei_damage" then
        local cardIds = room:askForDiscard(player, 1, 1, false, chongwei.name, true, nil, "#joy__powei-damage::" .. target.id, true)
        if #cardIds == 0 then
          return false
        end
        event:setCostData(self, cardIds[1])
      else
        event:setCostData(self, choice)
      end
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, chongwei.name)
      player:broadcastSkillInvoke(chongwei.name, 1)
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:setPlayerMark(p, "@@joy__powei_wei", 1)
      end
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    elseif event == fk.TurnStart then
      if target == player then
        if table.find(room.alive_players, function(p)
          return p:getMark("@@joy__powei_wei") > 0
        end) then
          room:notifySkillInvoked(player, chongwei.name)
          player:broadcastSkillInvoke(chongwei.name, 1)
          local hasLastPlayer = false
          for _, p in ipairs(room:getAlivePlayers()) do
            if p:getMark("@@joy__powei_wei") > (hasLastPlayer and 1 or 0) and not (#room.alive_players < 3 and p:getNextAlive() == player) then
              hasLastPlayer = true
              room:removePlayerMark(p, "@@joy__powei_wei")
              local nextPlayer = p:getNextAlive()
              if nextPlayer == player then
                nextPlayer = player:getNextAlive()
              end
              room:addPlayerMark(nextPlayer, "@@joy__powei_wei")
            else
              hasLastPlayer = false
            end
          end
        end
      end
      if type(event:getCostData(self)) == "number" then
        room:notifySkillInvoked(player, chongwei.name, "offensive")
        player:broadcastSkillInvoke(chongwei.name, 1)
        room:throwCard({ event:getCostData(self) }, chongwei.name, player, player)
        room:damage({
          from = player,
          to = target,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = chongwei.name,
        })
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      elseif event:getCostData(self) == "joy__powei_prey" then
        room:notifySkillInvoked(player, chongwei.name, "control")
        player:broadcastSkillInvoke(chongwei.name, 1)
        local cardId = room:askForCardChosen(player, target, "h", chongwei.name)
        room:obtainCard(player, cardId, false, fk.ReasonPrey)
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      end
    elseif event == fk.Damaged then
      room:setPlayerMark(target, "@@joy__powei_wei", 0)
    elseif event == fk.RoundEnd then
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    end
  end,
})
chongwei:addEffect(fk.Damaged, {
  name = "joy__chongwei",
  events = {fk.GameStart, fk.TurnStart, fk.Damaged,fk.RoundEnd},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if  not player:hasSkill(chongwei.name) or (#id == 0 and event ~= fk.GameStart) then
      return false
    end
    if event == fk.GameStart then
      return true
    elseif event == fk.TurnStart then
      return target == player or target:getMark("@@joy__powei_wei") > 0
    elseif event == fk.Damaged then
      return target:getMark("@@joy__powei_wei") > 0
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, nil)
    if event == fk.TurnStart and target:getMark("@@joy__powei_wei") > 0 then
      local room = player.room
      local choices = { "Cancel" }
      if target.hp <= player.hp and target ~= player and target:getHandcardNum() > 0 then
        table.insert(choices, 1, "joy__powei_prey")
      end
      if table.find(player:getCardIds(Player.Hand), function(id)
        return not player:prohibitDiscard(Fk:getCardById(id))
      end) then
        table.insert(choices, 1, "joy__powei_damage")
      end
      if #choices == 1 then
        return false
      end
      local choice = room:askForChoice(player, choices, chongwei.name)
      if choice == "Cancel" then
        return false
      end
      if choice == "joy__powei_damage" then
        local cardIds = room:askForDiscard(player, 1, 1, false, chongwei.name, true, nil, "#joy__powei-damage::" .. target.id, true)
        if #cardIds == 0 then
          return false
        end
        event:setCostData(self, cardIds[1])
      else
        event:setCostData(self, choice)
      end
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, chongwei.name)
      player:broadcastSkillInvoke(chongwei.name, 1)
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:setPlayerMark(p, "@@joy__powei_wei", 1)
      end
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    elseif event == fk.TurnStart then
      if target == player then
        if table.find(room.alive_players, function(p)
          return p:getMark("@@joy__powei_wei") > 0
        end) then
          room:notifySkillInvoked(player, chongwei.name)
          player:broadcastSkillInvoke(chongwei.name, 1)
          local hasLastPlayer = false
          for _, p in ipairs(room:getAlivePlayers()) do
            if p:getMark("@@joy__powei_wei") > (hasLastPlayer and 1 or 0) and not (#room.alive_players < 3 and p:getNextAlive() == player) then
              hasLastPlayer = true
              room:removePlayerMark(p, "@@joy__powei_wei")
              local nextPlayer = p:getNextAlive()
              if nextPlayer == player then
                nextPlayer = player:getNextAlive()
              end
              room:addPlayerMark(nextPlayer, "@@joy__powei_wei")
            else
              hasLastPlayer = false
            end
          end
        end
      end
      if type(event:getCostData(self)) == "number" then
        room:notifySkillInvoked(player, chongwei.name, "offensive")
        player:broadcastSkillInvoke(chongwei.name, 1)
        room:throwCard({ event:getCostData(self) }, chongwei.name, player, player)
        room:damage({
          from = player,
          to = target,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = chongwei.name,
        })
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      elseif event:getCostData(self) == "joy__powei_prey" then
        room:notifySkillInvoked(player, chongwei.name, "control")
        player:broadcastSkillInvoke(chongwei.name, 1)
        local cardId = room:askForCardChosen(player, target, "h", chongwei.name)
        room:obtainCard(player, cardId, false, fk.ReasonPrey)
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      end
    elseif event == fk.Damaged then
      room:setPlayerMark(target, "@@joy__powei_wei", 0)
    elseif event == fk.RoundEnd then
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    end
  end,
})
chongwei:addEffect(fk.RoundEnd, {
  name = "joy__chongwei",
  events = {fk.GameStart, fk.TurnStart, fk.Damaged,fk.RoundEnd},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local id = {}
      for _, p in ipairs(player.room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
    if  not player:hasSkill(chongwei.name) or (#id == 0 and event ~= fk.GameStart) then
      return false
    end
    if event == fk.GameStart then
      return true
    elseif event == fk.TurnStart then
      return target == player or target:getMark("@@joy__powei_wei") > 0
    elseif event == fk.Damaged then
      return target:getMark("@@joy__powei_wei") > 0
    else
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, nil)
    if event == fk.TurnStart and target:getMark("@@joy__powei_wei") > 0 then
      local room = player.room
      local choices = { "Cancel" }
      if target.hp <= player.hp and target ~= player and target:getHandcardNum() > 0 then
        table.insert(choices, 1, "joy__powei_prey")
      end
      if table.find(player:getCardIds(Player.Hand), function(id)
        return not player:prohibitDiscard(Fk:getCardById(id))
      end) then
        table.insert(choices, 1, "joy__powei_damage")
      end
      if #choices == 1 then
        return false
      end
      local choice = room:askForChoice(player, choices, chongwei.name)
      if choice == "Cancel" then
        return false
      end
      if choice == "joy__powei_damage" then
        local cardIds = room:askForDiscard(player, 1, 1, false, chongwei.name, true, nil, "#joy__powei-damage::" .. target.id, true)
        if #cardIds == 0 then
          return false
        end
        event:setCostData(self, cardIds[1])
      else
        event:setCostData(self, choice)
      end
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      room:notifySkillInvoked(player, chongwei.name)
      player:broadcastSkillInvoke(chongwei.name, 1)
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:setPlayerMark(p, "@@joy__powei_wei", 1)
      end
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    elseif event == fk.TurnStart then
      if target == player then
        if table.find(room.alive_players, function(p)
          return p:getMark("@@joy__powei_wei") > 0
        end) then
          room:notifySkillInvoked(player, chongwei.name)
          player:broadcastSkillInvoke(chongwei.name, 1)
          local hasLastPlayer = false
          for _, p in ipairs(room:getAlivePlayers()) do
            if p:getMark("@@joy__powei_wei") > (hasLastPlayer and 1 or 0) and not (#room.alive_players < 3 and p:getNextAlive() == player) then
              hasLastPlayer = true
              room:removePlayerMark(p, "@@joy__powei_wei")
              local nextPlayer = p:getNextAlive()
              if nextPlayer == player then
                nextPlayer = player:getNextAlive()
              end
              room:addPlayerMark(nextPlayer, "@@joy__powei_wei")
            else
              hasLastPlayer = false
            end
          end
        end
      end
      if type(event:getCostData(self)) == "number" then
        room:notifySkillInvoked(player, chongwei.name, "offensive")
        player:broadcastSkillInvoke(chongwei.name, 1)
        room:throwCard({ event:getCostData(self) }, chongwei.name, player, player)
        room:damage({
          from = player,
          to = target,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = chongwei.name,
        })
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      elseif event:getCostData(self) == "joy__powei_prey" then
        room:notifySkillInvoked(player, chongwei.name, "control")
        player:broadcastSkillInvoke(chongwei.name, 1)
        local cardId = room:askForCardChosen(player, target, "h", chongwei.name)
        room:obtainCard(player, cardId, false, fk.ReasonPrey)
        room:setPlayerMark(player, "joy__powei_debuff-turn", target.id)
      end
    elseif event == fk.Damaged then
      room:setPlayerMark(target, "@@joy__powei_wei", 0)
    elseif event == fk.RoundEnd then
      local id = {}
      for _, p in ipairs(room:getAlivePlayers()) do
        if p:getMark("@@joy__powei_wei") > 0 then
          table.insert(id,p)
        end
      end
      room:setPlayerMark(player, "joy__poweilose", #id)
    end
  end,
})

chongwei:addEffect("atkrange", {
  --FIXME!!!
  name = "#joy__chongwei-debuff",
  within_func = function (self, from, to)
    return to:getMark("joy__chongwei_debuff-turn") == from.id
  end,
})

return chongwei